﻿#region Copyright & License

//
// Author: Ian Davis <ian.f.davis@gmail.com> Copyright (c) 2007-2008, Ian Davs
//
// Portions of this software were developed for NUnit. See NOTICE.txt for more
// information. 
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.
//

#endregion

#region Using Directives

using System;

#endregion

namespace Ensurance.Constraints.Type
{
    /// <summary>
    /// TypeConstraint is the abstract base for constraints that take a Type as
    /// their expected value.
    /// </summary>
    public abstract class TypeConstraint : Constraint<System.Type>
    {
        /// <summary>
        /// The expected Type used by the constraint
        /// </summary>
        private System.Type _expectedType;

        /// <summary>
        /// Construct a TypeConstraint for a given Type
        /// </summary>
        /// <param name="type"></param>
        protected TypeConstraint(System.Type type)
        {
            _expectedType = type;
        }

        /// <summary>
        /// The expected Type used by the constraint
        /// </summary>
        protected internal System.Type ExpectedType
        {
            get { return _expectedType; }
            set { _expectedType = value; }
        }

        /// <summary>
        /// Write the actual value for a failing constraint test to a
        /// MessageWriter. TypeCOnstraints override this method to write the
        /// name of the type.
        /// </summary>
        /// <exception cref="ArgumentNullException">if the message writer is null.</exception>
        /// <param name="writer">The writer on which the actual value is displayed</param>
        public override void WriteActualValueTo(IMessageWriter writer)
        {
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }

            writer.WriteActualValue(Actual == null ? null : Actual.GetType());
        }
    }
}
